package com.nnfe.fyd.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Administrator on 2016/3/14 0014.
 * 和用户有关的sp
 */
public class SharePrefUtil {


    /**
     * shareprefence文件的 name
     */
    private final static String SP_NAME = "config_with_user";
    private final static String SP_NAME_WITHOUT_USER = "config_without_user";
    private static SharedPreferences sp1;
    private static SharedPreferences sp2;


    public static void saveBooleanWithoutUser(Context context, String key, boolean value) {
        if (sp2 == null)
            sp2 = context.getSharedPreferences(SP_NAME_WITHOUT_USER, 0);
        sp2 = context.getSharedPreferences(SP_NAME_WITHOUT_USER, Context.MODE_PRIVATE);
        sp2.edit().putBoolean(key, value).commit();
    }

    public static boolean getBooleanWithoutUser(Context context, String key, boolean defValue) {
        if (sp2 == null)
            sp2 = context.getSharedPreferences(SP_NAME_WITHOUT_USER, 0);
        return sp2.getBoolean(key, defValue);
    }


    /**
     * 保存布尔值
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveBoolean(Context context, String key, boolean value) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        // MODE_PRIVATE ： 0
        // MODE_WORLD_READABLE ： 1
        // MODE_WORLD_WRITEABLE： 2
        // MODE_APPEND               //该模式会检查文件是否存在，存在就往文件追加内容，否则就创建新文件
        // MODE_MULTI_PROCESS  ： 4  //android 2.3之后取消，代表多进程共享
        sp1 = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        sp1.edit().putBoolean(key, value).commit();
    }

    /**
     * 获取布尔值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static boolean getBoolean(Context context, String key, boolean defValue) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        return sp1.getBoolean(key, defValue);
    }


    /**
     * 保存字符串
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveString(Context context, String key, String value) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        sp1.edit().putString(key, value).commit();

    }

    /**
     * 获取字符值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static String getString(Context context, String key, String defValue) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        return sp1.getString(key, defValue);
    }

    /**
     * 获取字符值
     *
     * @param context
     * @param key
     * @return
     */
    public static String getString(Context context, String key) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        return sp1.getString(key, "");
    }

    /**
     * 保存long型
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveLong(Context context, String key, long value) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        sp1.edit().putLong(key, value).commit();
    }

    /**
     * 获取long值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static long getLong(Context context, String key, long defValue) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        return sp1.getLong(key, defValue);
    }


    /**
     * 保存int型
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveInt(Context context, String key, int value) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        sp1.edit().putInt(key, value).commit();
    }

    /**
     * 获取int值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static int getInt(Context context, String key, int defValue) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        return sp1.getInt(key, defValue);
    }


    /**
     * 保存float型
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveFloat(Context context, String key, float value) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        sp1.edit().putFloat(key, value).commit();
    }

    /**
     * 获取float值
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static float getFloat(Context context, String key, float defValue) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        return sp1.getFloat(key, defValue);
    }

    /**
     * 清除保存的所有数据
     *
     * @param context
     */
    public static void clear(Context context) {
        if (sp1 == null)
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        sp1.edit().clear().commit();
    }

    /**
     * 清除指定 key 的值
     *
     * @param context
     * @param key
     * @return
     */
    public static boolean removeString(Context context, String key) {
        if (sp1 == null) {
            sp1 = context.getSharedPreferences(SP_NAME, 0);
        }
        if (!sp1.contains(key)) {
            return false;
        }
        return sp1.edit().remove(key).commit();
    }


}
